package com.github.tvbox.osc.ui.activity

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.IntEvaluator
import android.animation.ObjectAnimator
import android.animation.TimeInterpolator
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.PathInterpolator
import me.jessyan.autosize.utils.AutoSizeUtils


/**
 * @ClassName: HomeAnim
 * @Description: TODO(动画)
 * @author: huangrongcai
 * @date: 2024/5/28
 *
 * @Copyright: 黄嵘才（HuangRc)
 */
public class HomeAnim {

    companion object {
        @JvmStatic
        public fun startAnimHide(view: View) {
            val show = view.visibility == View.VISIBLE
            val context = view.context
            val viewObj = ViewObj(view, view.getLayoutParams() as ViewGroup.MarginLayoutParams)
            val animatorSet = AnimatorSet()
            val listener = object : Animator.AnimatorListener {
                override fun onAnimationStart(animation: Animator) {
                }

                override fun onAnimationEnd(animation: Animator) {
                    view.visibility = View.GONE
                }

                override fun onAnimationCancel(animation: Animator) {
                }

                override fun onAnimationRepeat(animation: Animator) {
                }
            }
            animatorSet.addListener(listener)
            if (show) {
                animatorSet.playTogether(

                    ObjectAnimator.ofObject(
                        viewObj,
                        "marginTop",
                        IntEvaluator(),
                        Integer.valueOf(AutoSizeUtils.mm2px(context, 10.0f)),
                        Integer.valueOf(AutoSizeUtils.mm2px(context, 0.0f))
                    ), ObjectAnimator.ofObject(
                        viewObj,
                        "height",
                        IntEvaluator(),
                        Integer.valueOf(AutoSizeUtils.mm2px(context, 50.0f)),
                        Integer.valueOf(AutoSizeUtils.mm2px(context, 1.0f))
                    ), ObjectAnimator.ofFloat(view, "alpha", 1.0f, 0.0f)
                )

                animatorSet.duration = 200
                animatorSet.start()
            }
        }

        @JvmStatic
        public fun startAnimShow(view: View) {
            val show = view.visibility == View.VISIBLE
            val context = view.context;
            val viewObj = ViewObj(view, view.getLayoutParams() as ViewGroup.MarginLayoutParams)
            val animatorSet = AnimatorSet()
            val listener = object : Animator.AnimatorListener {
                override fun onAnimationStart(animation: Animator) {
//                    view.visibility = View.VISIBLE
                }

                override fun onAnimationEnd(animation: Animator) {
                    view.visibility = View.VISIBLE
                }

                override fun onAnimationCancel(animation: Animator) {
                }

                override fun onAnimationRepeat(animation: Animator) {
                }
            }
            animatorSet.addListener(listener)

            if (!show) {
                val marginTop = ObjectAnimator.ofObject(
                    viewObj,
                    "marginTop",
                    IntEvaluator(),
                    Integer.valueOf(AutoSizeUtils.mm2px(context, 0.0f)),
                    Integer.valueOf(AutoSizeUtils.mm2px(context, 10.0f))
                )

                val height = ObjectAnimator.ofObject(
                    viewObj,
                    "height",
                    IntEvaluator(),
                    Integer.valueOf(AutoSizeUtils.mm2px(context, 1.0f)),
                    Integer.valueOf(AutoSizeUtils.mm2px(context, 50.0f))
                )

                val alpha = ObjectAnimator.ofFloat(view, "alpha", 0.0f, 1.0f)
                animatorSet.playTogether(
                    marginTop, height, alpha

                )
                // 定义贝塞尔曲线插值器
                val pathInterpolator: TimeInterpolator =
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        PathInterpolator(0.42f, 0.0f, 0.58f, 1.0f)
                    } else {
                        AccelerateDecelerateInterpolator()
                    }
                animatorSet.duration = 200L
                animatorSet.interpolator = pathInterpolator
                animatorSet.start()
            }
        }
    }
}

class ViewObj(private val view: View, private val params: ViewGroup.MarginLayoutParams) {
    fun setMarginLeft(left: Int) {
        params.leftMargin = left
        view.layoutParams = params
    }

    fun setMarginTop(top: Int) {
        params.topMargin = top
        view.layoutParams = params
    }

    fun setMarginRight(right: Int) {
        params.rightMargin = right
        view.layoutParams = params
    }

    fun setMarginBottom(bottom: Int) {
        params.bottomMargin = bottom
        view.layoutParams = params
    }

    fun setWidth(width: Int) {
        params.width = width
        view.layoutParams = params
    }

    fun setHeight(height: Int) {
        params.height = height
        view.layoutParams = params
    }
}